<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\CarModel;


/**
 *    品牌管理
 */
class CarModelController extends BaseController
{
    public $cm = null;
    public $can_view = null;
    public $can_manage = null;

    //ublic
    public function __construct()
    {
        parent::__construct();
        $cm_db = C('DB_CAR_MODEL');
        $cm_dsn = $cm_db['DB_TYPE'] . "://" . $cm_db['DB_USER'] . ":" . $cm_db['DB_PWD'] . "@" . $cm_db['DB_HOST'] . ":" . $cm_db['DB_PORT'] . "/" . $cm_db['DB_NAME'];
        $this->cm = new CarModel($cm_db['DB_PREFIX'], $cm_dsn);
        $this->first = $this->cm->getFirstChar();
        $this->emp_id = $this->user_info['emp_id'];
        //权限
        $e = new \AuctionCore\Account\Back\Employee($this->user_info['username']);

        $this->can_view = $e->check_auth("CheXingKuChaKan");    //查看权限
        $this->can_manage = $e->check_auth("CheXingKuGuanLi");        //管理权限

        if (empty($this->can_view) && empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }

        $this->assign('can_manage', $this->can_manage);
    }

    //------------------------------------------主品牌管理---------------------------------------
    //主品牌
    public function index()
    {
        $curr_char = I('get.char', 'A');
        $manufacturer = $this->cm->get_manufacturer();
        $this->assign('manufacturer', $manufacturer);
        $this->assign('curr_char', $curr_char);
        $this->assign('first', $this->first);
        $this->assign('status', array('0' => '启用', '1' => '禁用', '-1' => '删除'));//启用禁用状态
        $this->display();
    }

    //主品牌、品牌、车系、车型列表
    public function get_list()
    {

        $p = I("get.p", 1);
        $curr_char = I('get.char', 'A');

        $size = 20;
        $map = array();

        $type = I('get.type', 'mbrand');
        switch ($type) {
            case "mbrand":
                $map['first_char'] = array('eq', $curr_char);
                $callback = 'master_list';
                break;
            case "brand":
                //$map['first_char'] = array('eq',$curr_char);
                $mbrand_id = I('get.mbrand_id/d');
                $map['mbrand_id'] = array('eq', $mbrand_id);
                $callback = 'brand_list';
                break;
            case "series":
                $brand_id = I('get.brand_id/d');
                $map['brand_id'] = array('eq', $brand_id);
                $callback = 'series_list';
                break;
            case "model":
                $series_id = I('get.series_id/d');
                $map['series_id'] = array('eq', $series_id);
                $callback = 'model_list';
                break;
        }
        $json = I('get.json');
        if ($json) {
            $map['status'] = 0;
            $ret = $this->cm->get_data(1, 10000, $map, $type);
            $this->helpJsonResult('200', 'SUCCESS', $ret);
            exit;
        } else {
            $map['status'] = array('gt', -1);
            $ret = $this->cm->get_data($p, $size, $map, $type);
        }
        //echo M()->getLastSql();
        $list = $ret[0];
        $total = intval($ret[1]);
        $Page = new \AuctionCore\Common\AjaxPage($total, $size, $callback);
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $p);
        $this->assign('curr_char', $curr_char);
        $this->display($callback);
    }


    //------------------------------添加/修改主品牌、品牌、车系、车型-------------------------
    //添加/修改主品牌
    public function master_add()
    {

        //权限判断
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }

        $first_char_master = I('post.first_char_master');
        $country_master = I('post.country_master');
        $name_master = I('post.name_master');
        $old_name_master = I('post.old_name_master');
        $spell_master = I('post.spell_master');
        //$status_master = I('post.status_master');
        $mbrand_id = I('post.mbrand_id');

        if (empty($first_char_master)) {
            $this->helpJsonResult('701', '主品牌首字母不能为空');
            exit;
        }
        if (empty($country_master)) {
            $this->helpJsonResult('702', '主品牌国家不能为空');
            exit;
        }
        if (empty($name_master)) {
            $this->helpJsonResult('703', '主品牌名称不能为空');
            exit;
        }
        if (empty($spell_master)) {
            $this->helpJsonResult('704', '主品牌全拼不能为空');
            exit;
        }
        $data = array();
        $data['first_char'] = $first_char_master;
        $data['country'] = $country_master;
        $data['name'] = trim($name_master);
        $data['spell'] = trim($spell_master);
        //$data['status'] = $status_master;//默认开启
        //名称和拼音不可重复
        $map = array();
        $map['name'] = $data['name'];
        if ($mbrand_id > 0) {
            if ($data['name'] != $old_name_master) {
                $c = $this->cm->get_data(1, 20, $map);
                if (intval($c[1]) > 0) {
                    $this->helpJsonResult('708', '主品牌已存在');
                    exit;
                }
            }
        } else {
            $c = $this->cm->get_data(1, 20, $map);
            if (intval($c[1]) > 0) {
                $this->helpJsonResult('708', '主品牌已存在');
                exit;
            }
        }
        //入库
        if ($mbrand_id > 0) {
            $ret = $this->cm->update($data, array('mbrand_id' => array('eq', $mbrand_id)));
        } else {
            $ret = $this->cm->add($data);
            $data['mbrand_id'] = $ret;
        }
        if ($ret === false) {
            $code = '705';
            $msg = $mbrand_id > 0 ? '修改失败' : '添加失败';
        } else {
            $code = '200';
            $msg = $mbrand_id > 0 ? '修改成功' : '添加成功';

            //同步多个其它数据库
            $table = 'car_mbrand';
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    if ($mbrand_id > 0) {
                        $this->cm->sync_update($data, array('mbrand_id' => array('eq', $mbrand_id)), $table, C($v)['DB_PREFIX'], $DSN);
                    } else {
                        $this->cm->sync_add($data, $table, C($v)['DB_PREFIX'], $DSN);
                    }
                }
            }

        }
        //log
        $log = array('emp_id' => $this->emp_id, 'action' => ($mbrand_id > 0 ? '修改' : '添加') . '主品牌' . $name_master . '。' . $msg);
        \AuctionCore\System\SystemLog::add($log);

        //json
        $this->helpJsonResult($code, $msg);

    }

    //添加/修改品牌
    public function brand_add()
    {
        //权限判断
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }

        $first_char_master = I('post.first_char_brand');
        $mbrand_id = I('post.mbrand_id');
        $name = I('post.name_brand');
        $old_name = I('post.old_name_brand');
        $spell = I('post.spell_brand');
        //$status = I('post.status_brand','1');

        $manufacturer_id = I('post.manufacturer_id');

        $brand_id = I('post.brand_id');

        if (empty($first_char_master)) {
            $this->helpJsonResult('701', '主品牌首字母不能为空');
            exit;
        }
        if (empty($mbrand_id)) {
            $this->helpJsonResult('711', '请选择主品牌');
            exit;
        }
        if (empty($manufacturer_id)) {
            $this->helpJsonResult('714', '请选择厂商');
            exit;
        }
        if (empty($name)) {
            $this->helpJsonResult('712', '请填写品牌名称');
            exit;
        }
        if (empty($spell)) {
            $this->helpJsonResult('713', '品牌全拼不能为空');
            exit;
        }

        $data = array();
        $data['first_char'] = $first_char_master;
        $data['manufacturer_id'] = $manufacturer_id;
        $data['mbrand_id'] = $mbrand_id;
        $data['name'] = trim($name);
        $data['spell'] = trim($spell);
        //$data['status'] = $status;//默认开启
        //排重
        $map = array();
        $map['name'] = $data['name'];
        if ($brand_id > 0) {
            if ($data['name'] != $old_name) {
                $c = $this->cm->get_data(1, 20, $map, 'brand');
                if (intval($c[1]) > 0) {
                    $this->helpJsonResult('708', '该品牌已存在');
                    exit;
                }
            }
        } else {
            $c = $this->cm->get_data(1, 20, $map, 'brand');
            if (intval($c[1]) > 0) {
                $this->helpJsonResult('708', '该品牌已存在');
                exit;
            }
        }

        //根据厂商信息获取国家信息
        $manufacturer = $this->cm->get_manufacturer_info($manufacturer_id);
        $data['country'] = $manufacturer['country'];;

        //入库
        if ($brand_id > 0) {
            $ret = $this->cm->update($data, array('brand_id' => array('eq', $brand_id)), 'car_brand');
        } else {
            $ret = $this->cm->add($data, 'car_brand');
            $data['brand_id'] = $ret;
        }
        if ($ret === false) {
            $code = '705';
            $msg = $brand_id > 0 ? '修改失败' : '添加失败';
        } else {
            $code = '200';
            $msg = $brand_id > 0 ? '修改成功' : '添加成功';

            //同步多个其它数据库
            $table = 'car_brand';
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    if ($brand_id > 0) {
                        $this->cm->sync_update($data, array('brand_id' => array('eq', $brand_id)), $table, C($v)['DB_PREFIX'], $DSN);
                    } else {
                        $this->cm->sync_add($data, $table, C($v)['DB_PREFIX'], $DSN);
                    }
                }
            }
        }
        //log
        $log = array('emp_id' => $this->emp_id, 'action' => ($brand_id > 0 ? '修改' : '添加') . '品牌[' . $name . ']。' . $msg);
        \AuctionCore\System\SystemLog::add($log);

        //json
        $this->helpJsonResult($code, $msg);

    }

    //添加/修改车系
    public function series_add()
    {
        //权限判断
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }
        $brand_id = I('post.brand_id/d');
        $sale_status = I('post.sale_status/d');
        $type = I('post.type/d');
        $name = trim(I('post.name'));
        $old_name = I('post.old_name');
        $spell = trim(I('post.spell'));
        $desc = trim(I('post.desc'));
        //$status = I('post.status','1');
        $series_id = I('post.series_id');

        if (empty($brand_id)) {
            $this->helpJsonResult('604', '品牌不能为空');
            exit;
        }
        if (empty($sale_status)) {
            $this->helpJsonResult('802', '销售状态不能为空');
            exit;
        }
        if (empty($type)) {
            $this->helpJsonResult('803', '车系类型不能为空');
            exit;
        }
        if (empty($name)) {
            $this->helpJsonResult('804', '车系名称不能为空');
            exit;
        }
        if (empty($spell)) {
            $this->helpJsonResult('805', '车系全拼不能为空');
            exit;
        }

        $data = array();
        $data['brand_id'] = $brand_id;
        $data['sale_status'] = $sale_status;
        $data['type'] = $type;
        $data['name'] = $name;
        $data['describe'] = $desc;
        $data['spell'] = $spell;
        //$data['status'] = $status;//默认开启
        //排重
        $map = array();
        $map['name'] = $data['name'];
        if ($series_id > 0) {
            if ($data['name'] != $old_name) {
                $c = $this->cm->get_data(1, 20, $map, 'series');
                if (intval($c[1]) > 0) {
                    $this->helpJsonResult('708', '车系已存在');
                    exit;
                }
            }
        } else {
            $c = $this->cm->get_data(1, 20, $map, 'series');
            if (intval($c[1]) > 0) {
                $this->helpJsonResult('708', '车系已存在');
                exit;
            }
        }
        //入库
        if ($series_id > 0) {
            $ret = $this->cm->update($data, array('series_id' => array('eq', $series_id)), 'car_series');
        } else {
            $ret = $this->cm->add($data, 'car_series');
            $data['series_id'] = $ret;
        }
        if ($ret === false) {
            $code = '705';
            $msg = $series_id > 0 ? '修改失败' : '添加失败';
        } else {
            $code = '200';
            $msg = $series_id > 0 ? '修改成功' : '添加成功';

            //同步多个其它数据库
            $table = 'car_series';
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    if ($series_id > 0) {
                        $this->cm->sync_update($data, array('series_id' => array('eq', $series_id)), $table, C($v)['DB_PREFIX'], $DSN);
                    } else {
                        $this->cm->sync_add($data, $table, C($v)['DB_PREFIX'], $DSN);
                    }
                }
            }

        }
        //log
        $log = array('emp_id' => $this->emp_id, 'action' => ($series_id > 0 ? '修改' : '添加') . '车系' . $name . '。' . $msg);
        \AuctionCore\System\SystemLog::add($log);

        //json
        $this->helpJsonResult($code, $msg);

    }

    //添加/修改车型
    public function model_add()
    {
        //权限判断
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }
        if (!IS_POST) {
            //配置参数
            $params = $this->cm->getModelCols();

            //
            $model_id = I('get.id/d');
            if ($model_id > 0) {
                $detail = $this->cm->get_model_by_id($model_id);

                //参数修正
                $config = json_decode($detail['detail_params'], true);
                $config = $config['vehicle_config'];
                $config = $this->cm->configParse($config);
                $this->assign('config', $config);

                //根据车系获取主品牌首字母、主品牌及品牌
                $series = $this->cm->get_model_by_id($detail['series_id'], 'series');
                $brand = $this->cm->get_model_by_id($series['brand_id'], 'brand');
                $detail['first_char'] = $brand['first_char'];
                $detail['mbrand_id'] = $brand['mbrand_id'];
                $detail['brand_id'] = $brand['brand_id'];
            }

            //模板变量
            $this->assign('detail', $detail);
            $this->assign('patams', $params);
            $this->assign('first', $this->first);
            $this->display();
            exit;
        }

        $series_id = I('post.series_id');
        $name = urldecode(I('post.name'));
        $old_name = urldecode(I('post.old_name'));

        $produce_status = I('post.produce_status');
        $sale_status = I('post.sale_status');
        $air_inlet_form = I('post.air_inlet_form');
        $gearbox = I('post.gearbox');
        $year = I('post.year/d');
        $gear = I('post.gear/d');
        $car_cc = I('post.car_cc');
        $consumption = I('post.consumption');
        $refer_price = I('post.refer_price');

        //
        $model_id = I('post.model_id/d');
        if (empty($series_id)) {
            $this->helpJsonResult('605', '车系不能为空');
            exit;
        }
        if (empty($name)) {
            $this->helpJsonResult('901', '车型名称不能为空');
            exit;
        }
        if (empty($year)) {
            $this->helpJsonResult('902', '年款不能为空');
            exit;
        }
        if (empty($gear)) {
            $this->helpJsonResult('903', '档位数不能为空');
            exit;
        }
        if (empty($car_cc)) {
            $this->helpJsonResult('904', '排量不能为空');
            exit;
        }
//        if(empty($consumption)){
//            $this->helpJsonResult('905', '油耗不能为空');
//            exit;
//        }
//        if(empty($refer_price)){
//            $this->helpJsonResult('906', '厂商指导价不能为空');
//            exit;
//        }
        $data = array();

        $data['series_id'] = $series_id;
        $data['name'] = $name;

        $data['produce_status'] = $produce_status;
        $data['sale_status'] = $sale_status;
        $data['air_inlet_form'] = $air_inlet_form;

        $data['gearbox'] = $gearbox;
        $data['year'] = $year;
        $data['gear'] = $gear;

        $data['car_cc'] = trim($car_cc);
        $data['consumption'] = trim($consumption);
        $data['refer_price'] = trim($refer_price);

        $data['status'] = 0;//默认开启

        //详细配置
        $vehicle_config = array();
        $detail_params = explode('&', urldecode(I('post.detail_params')));
        foreach ($detail_params as $v) {
            $tmp = explode('=', $v);
            $vehicle_config[$tmp[0]] = $tmp[1];
        }
        $detail_params = array('vehicle_config' => $vehicle_config);
        //排重
        $map = array();
        $map['name'] = $data['name'];
        $map['year'] = $year;
        if ($model_id > 0) {

            if ($data['name'] != $old_name) {
                $c = $this->cm->get_data(1, 20, $map, 'model');
                if (intval($c[1]) > 0) {
                    $this->helpJsonResult('708', '车型已存在');
                    exit;
                }
            }
        } else {
            $c = $this->cm->get_data(1, 20, $map, 'model');
            if (intval($c[1]) > 0) {
                $this->helpJsonResult('708', '车型已存在');
                exit;
            }
        }
        //入库
        if ($model_id > 0) {
            $arr = $this->cm->get_model_by_id($model_id);
            $old = json_decode($arr['detail_params'], true);
            $old = $old['vehicle_config'];
            foreach ($detail_params['vehicle_config'] AS $key => $val) {
                $old[$key] = $val;
            }
            $data['detail_params'] = json_encode(array('vehicle_config' => $old));

            $ret = $this->cm->update($data, array('model_id' => array('eq', $model_id)), 'car_model');
        } else {
            $data['from_type'] = 1;
            $data['detail_params'] = json_encode($detail_params);
            $ret = $this->cm->add($data, 'car_model');
            $data['model_id'] = $ret;
        }
        if ($ret === false) {
            $code = '705';
            $msg = $model_id > 0 ? '修改失败' : '添加失败';
        } else {
            $code = '200';
            $msg = $model_id > 0 ? '修改成功' : '添加成功';

            //同步多个其它数据库
            $table = 'car_model';
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    if ($model_id > 0) {
                        $this->cm->sync_update($data, array('model_id' => array('eq', $model_id)), $table, C($v)['DB_PREFIX'], $DSN);
                    } else {
                        $this->cm->sync_add($data, $table, C($v)['DB_PREFIX'], $DSN);
                    }
                }
            }

        }
        //log
        $log = array('emp_id' => $this->emp_id, 'action' => ($model_id > 0 ? '修改' : '添加') . '车型' . $name . '。' . $msg);
        \AuctionCore\System\SystemLog::add($log);

        //json
        $this->helpJsonResult($code, $msg, $ret);

    }

    //禁用，启用
    public function toggle()
    {
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }

        $id = I('post.id');
        $table = I('post.table');
        $type = I('post.type');
        $data = array('status' => $type);
        switch ($table) {
            case "mbrand":
                $key = 'mbrand_id';
                break;
            case "brand":
                $key = 'brand_id';
                break;
            case "series":
                $key = 'series_id';
                break;
            case "model":
                $key = 'model_id';
                break;
            default:
                break;
        }
        $rs = $this->cm->update($data, array($key => array('eq', $id)), 'car_' . $table);
        if ($rs) {
            //同步多个其它数据库
            $table = 'car_' . $table;
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    if ($id > 0) {
                        $rs = $this->cm->sync_update($data, array($key => array('eq', $id)), $table, C($v)['DB_PREFIX'], $DSN);
                    } else {
                        $rs = $this->cm->sync_add($data, $table, C($v)['DB_PREFIX'], $DSN);
                    }
                }
            }
            //log
            $log = array('emp_id' => $this->emp_id, 'action' => ($type > 0 ? '禁用' : '启用') . ' table:car_' . $table . ' id:' . $id . '。');
            \AuctionCore\System\SystemLog::add($log);
        } else {
            //json
            $this->helpJsonResult('409', '操作失败');
        }
        //json
        $this->helpJsonResult('200', 'SUCCESS');

    }

    //----------------------------主品牌信息、品牌信息、车系信息、车型信息---------------
    public function base_info()
    {
        $id = I('get.id/d');
        $type = I('get.type');//mbrand,brand,series,model
        $info = $this->cm->get_model_by_id($id, $type);
        if ($type == 'series') {
            $brand = $this->cm->get_model_by_id($info['brand_id'], 'brand');
            $info['first_char'] = $brand['first_char'];
            $info['mbrand_id'] = $brand['mbrand_id'];
        }
        $this->helpJsonResult(200, 'SUCCESS', $info);
    }

    //------------------------------移除主品牌、品牌、车系、车型---------------------
    public function remove()
    {
        if (empty($this->can_manage)) {
            $this->helpJsonResult('2001', '操作无权限');
            exit;
        }

        $id = I('post.id/d');
        $type = I('post.type');//mbrand,brand,series,model
        if (empty($id)) {
            $this->helpJsonResult('202', 'ID不能为空');
            exit;
        }
        $data = array();
        $data['status'] = '1';
        $map = array();
        switch ($type) {
            case "mbrand":
                $map['mbrand_id'] = array('eq', $id);
                $action = '修改主品牌[mbrand_id:' . $id . ']状态为禁用。';
                $table = 'car_mbrand';
                break;
            case "brand":
                $map['brand_id'] = array('eq', $id);
                $action = '修改品牌[brand_id:' . $id . ']状态为禁用。';
                $table = 'car_brand';
                break;
            case "series":
                $map['series_id'] = array('eq', $id);
                $action = '修改车系[series_id:' . $id . ']状态为禁用。';
                $table = 'car_series';
                break;
            case "model":
                $map['model_id'] = array('eq', $id);
                $action = '修改车型[model_id:' . $id . ']状态为禁用。';
                $table = 'car_model';
                break;
        }
        $ret = $this->cm->update($data, $map, $table);
        if ($ret === false) {
            $code = '707';
            $msg = '移除失败';
        } else {
            $code = '200';
            $msg = '移除成功';

            //同步多个其它数据库
            foreach (C('DB_TO_SYNC') as $v) {
                if ($v) {
                    $DSN = C($v)['DB_TYPE'] . "://" . C($v)['DB_USER'] . ":" . C($v)['DB_PWD'] . "@" . C($v)['DB_HOST'] . ":" . C($v)['DB_PORT'] . "/" . C($v)['DB_NAME'];
                    $this->cm->sync_update($data, $map, $table, C($v)['DB_PREFIX'], $DSN);
                }
            }
        }
        //log
        $log = array('emp_id' => $this->emp_id, 'action' => $action . $msg);
        \AuctionCore\System\SystemLog::add($log);

        //json
        $this->helpJsonResult($code, $msg);

    }
}
